#include "GraficoIntel.h"

bool ComparadorByte(Byte_Tiempo& a,Byte_Tiempo& b)
{
    return a.Byte < b.Byte;
}

bool ComparadorTiempo(Byte_Tiempo& a,Byte_Tiempo& b)
{
    return a.Time < b.Time;
}

void GraficoIntel::setearEscala()
{
    this->ESCALA_BYTE_PIXEL = 1 ;
    this->ESCALA_TIEMPO_PIXEL = 1 ;
    std::list<Byte_Tiempo>::iterator it = this->CantBytesPorUdeT.begin();
    std::list<Byte_Tiempo>::iterator itEnd = this->CantBytesPorUdeT.end();

    int MaxT = ((*std::max_element( it , itEnd,ComparadorTiempo ))).Time;
    if ( MaxT != 0)
    {
        this->ESCALA_TIEMPO_PIXEL = float(MaxT-this->offset_x) / (this->width);
    }

    int MaxByte = ((*std::max_element( it , itEnd,ComparadorByte ))).Byte;
    if ( MaxByte != 0)
    {
        this->ESCALA_BYTE_PIXEL = float(MaxByte) / (this->height);
    }
}
bool GraficoIntel::on_expose_event(GdkEventExpose* event)
{
  // This is where we draw on the window
  Glib::RefPtr<Gdk::Window> window = get_window();

  if( (window) && (this->CantBytesPorUdeT.size() != 0 ) )
  {
    Gtk::Allocation allocation = get_allocation();
    this->width = allocation.get_width();
    this->height = allocation.get_height();
    Cairo::RefPtr<Cairo::Context> cr = window->create_cairo_context();
    Glib::RefPtr<Pango::Layout> pangoLayout = Pango::Layout::create (cr);

    // clip to the area indicated by the expose event so that we only redraw
    // the portion of the window that needs to be redrawn
    cr->rectangle(event->area.x, event->area.y,event->area.width, event->area.height);
    cr->clip();
    //**********************************************************************
    cr->set_source_rgb(0.0, 0.0, 0.6);
    cr->move_to( width-150 , height-20 );
    pangoLayout->set_text( "Tiempo(segundos)" );
    pangoLayout->update_from_cairo_context(cr);  //gets cairo cursor position
    pangoLayout->add_to_cairo_context(cr);
    cr->stroke();
    cr->move_to( 5 , 0 );
    pangoLayout->set_text( "Bytes" );
    pangoLayout->update_from_cairo_context(cr);  //gets cairo cursor position
    pangoLayout->add_to_cairo_context(cr);
    cr->stroke();
    //**********************************************************************
    this->width = this->width-75;
    //magic number para que los textos queden bien en la pantalla
    this->setearEscala();
    this->height = this->height+30;
    /*std::cout << "height: " << this->height << std::endl;
    std::cout << "width: " << this->width << std::endl;
    std::cout << "esc tiempo: " << this->ESCALA_TIEMPO_PIXEL << std::endl;
    std::cout << "esc byte: " << this->ESCALA_BYTE_PIXEL << std::endl;*/

    std::list<Byte_Tiempo>::iterator it = this->CantBytesPorUdeT.begin();
    std::list<Byte_Tiempo>::iterator itEnd = this->CantBytesPorUdeT.end();
    /*The origin of the Cairo coordinate system is
    located in the upper-left corner of the window with positive x values
    to the right and positive y values going down. */
    cr->set_line_width(2.0) ;
    cr->set_source_rgb(0.0, 0.0, 0.6);
    cr->move_to( ((*it).Time - this->offset_x )/ ESCALA_TIEMPO_PIXEL , height - ( (*it).Byte / ESCALA_BYTE_PIXEL ) );
    this->DrawTextOnPointOfGraph(cr,pangoLayout,*it);
    it++;
    while( it != itEnd )
    {
        Byte_Tiempo BT = *it;
        cr->line_to( (BT.Time - this->offset_x )/ ESCALA_TIEMPO_PIXEL, height - ( (BT.Byte) / ESCALA_BYTE_PIXEL ) );
        cr->stroke();
        cr->move_to( (BT.Time - this->offset_x )/ ESCALA_TIEMPO_PIXEL , height - ( (BT.Byte) / ESCALA_BYTE_PIXEL ) );
        this->DrawTextOnPointOfGraph(cr,pangoLayout,BT);
        it++;
    }
  }
  return true;
}

void GraficoIntel::DrawTextOnPointOfGraph(Cairo::RefPtr<Cairo::Context> cr
                                          ,Glib::RefPtr<Pango::Layout> pangoLayout
                                          ,const Byte_Tiempo& BT  )
{
    std::string str = BT.to_string();
    cr->set_source_rgb(0.0, 1.0, 0.0);
    pangoLayout->set_text( str );
    pangoLayout->update_from_cairo_context(cr);  //gets cairo cursor position
    pangoLayout->add_to_cairo_context(cr);
    cr->stroke();
    cr->set_source_rgb(0.0, 0.0, 0.0);
    cr->move_to( (BT.Time - this->offset_x ) / ESCALA_TIEMPO_PIXEL , height - ( BT.Byte / ESCALA_BYTE_PIXEL ) );
}
